package com.jyh.service.impl;

import com.jyh.dao.UserDao;
import com.jyh.entity.User;
import com.jyh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author JYH
 * @version 1.0
 * @date 2022/12/7 13:27
 */
@Service
public class UserServiceImpl2 implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    @Cacheable(cacheNames = "redis::user",key = "#uid")
    public User findByUid(Integer uid) {
        User user = userDao.selectById(uid);
        return user;
    }

    @Override
    public User save(User user) {
        int i = userDao.insert(user);
        return user;
    }

    @Override
    @CacheEvict(cacheNames = "redis::user",key = "#uid")
    public int deleteByUid(Integer uid) {

        //删除数据库中的key
        int i = userDao.deleteById(uid);
        return i;
    }

    @Override
    @CachePut(cacheNames = "redis::user",key = "#user.uid")
    public User updateByUid(User user) {
        //修改数据库中的key
        int i = userDao.updateById(user);
        return user;
    }
}
